﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization.Formatters.Soap;
using System.Text;
using BigfootSQL.Json;

namespace BigfootSQL
{
    class Utils
    {
        #region "Serilizers"

        public static string XmlSerialize(object data)
        {
            var ser = new SoapFormatter();
            using (var ms = new MemoryStream())
            {
                ser.Serialize(ms, data);
                ms.Flush();
                ms.Position = 0;
                using (var sr = new StreamReader(ms))
                {
                    return sr.ReadToEnd();
                }
            }
        }

        public static object XmlDerialize(string data)
        {
            var ser = new SoapFormatter();
            using (var ms = new MemoryStream())
            {
                using (var sw = new StreamWriter(ms))
                {
                    sw.Write(data);
                    sw.Flush();
                    ms.Position = 0;
                    return ser.Deserialize(ms);
                }
            }

        }

        public static string JsonSerialize(object data)
        {
            return JsonSerialize(data, false);
        }

        public static string JsonSerialize(object data, bool ConvertToBase64)
        {
            var sb = new StringBuilder();
            var tr = new StringWriter(sb);
            var serializer = new JsonSerializer();
            serializer.Serialize(tr, data);
            var jsonString = sb.ToString();
            if (ConvertToBase64) jsonString = ToBase64(jsonString);
            return jsonString;
        }

        public static T JsonDeserialize<T>(string data)
        {
            return JsonDeserialize<T>(data, false);
        }

        public static T JsonDeserialize<T>(string data, bool ConvertFromBase64)
        {
            if (ConvertFromBase64) data = FromBase64(data);
            var serializer = new JsonSerializer();
            return (T)serializer.Deserialize(new JsonReader(new StringReader(data)), typeof(T));
        }

        public static string ToBase64(string data)
        {
            var enc = new UTF8Encoding();
            return Convert.ToBase64String(enc.GetBytes(data));
        }

        public static string FromBase64(string data)
        {
            var enc = new UTF8Encoding();
            return enc.GetString(Convert.FromBase64String(data));
        }

        public static string BinSerialize(object data)
        {
            var ser = new BinaryFormatter();
            using (var ms = new MemoryStream())
            {
                ser.Serialize(ms, data);
                ms.Flush();
                ms.Position = 0;
                return Convert.ToBase64String(ms.ToArray());
            }
        }

        public static T BinDeserialize<T>(string base64string)
        {
            using (var ms = new MemoryStream(Convert.FromBase64String(base64string)))
            {
                ms.Position = 0;
                var ser = new BinaryFormatter();
                return (T)ser.Deserialize(ms);
            }
        }

        #endregion
    }
}
